package com.donger.common.core.validator.customrule;

import com.donger.common.core.annotation.Contain;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * ContainValidator
 * <p>
 *     这个是自定义校验规则@Contain的实现.
 *     使用请参考{@link Contain}
 * </p>
 *
 * @author xyx
 * @date 2020-03-13 16:36
 */
public class ContainValidator implements ConstraintValidator<Contain, String> {

    private String includeStr;
    private boolean isConvert;

    @Override
    public void initialize(Contain constraintAnnotation) {
        this.isConvert = constraintAnnotation.convert();
        this.includeStr = constraintAnnotation.include();
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        if (value == null) {
            return true;
        }
        boolean contains = value.contains(includeStr);
        return isConvert != contains;
    }

}
